package com.cszjzx.member.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.cszjzx.member.po.Major;

public class MajorDAOImpl implements MajorDAO ,RowMapper{
    private JdbcHelper helper ;
    public MajorDAOImpl() throws IOException {
    	helper=new JdbcHelper();
    }
	@Override
	public void add(Major c) throws SQLException {
	    String sql="insert into major (name,departmentid) value (?,?)";
	    Object []args={c.getName(),c.getDepartmentId()};
	    helper.executeUpdate(sql, args);
	}

	@Override
	public void dalete(int id) throws SQLException {
		String sql="delete from major where id=?";
		Object []args={id};
		helper.executeUpdate(sql, args);
	}

	public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
		Major major =new Major();
		major.setDepartmentId(rs.getInt("major.departmentId"));
		major.setName(rs.getString("major.name"));
		major.setId(rs.getInt("major.id"));
		major.setParentLeague("departmen.name");
		return major;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Major> findAll() throws ClassNotFoundException, SQLException, IOException {
        String sql="select * from major,department where major.departmentid= department.id";
        Object[]args={};
		return helper.executeQuery(sql, args, this);
	}
	@SuppressWarnings("unchecked")
	@Override
    public List<Major> findByDepartmentId(int departmentId) throws ClassNotFoundException, SQLException, IOException {
		String sql="select * from major,department where major.departmentid= department.id and major.departmentid=?";
	    Object[]args={departmentId};
	    return helper.executeQuery(sql, args, this);
    }
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
		
	    /*List <Major> list= new MajorDAOImpl().findByDepartmentName("传媒系");
	    for(int i=0;i<list.size();i++)
	    	System.out.println(list.get(i));
	    }*/
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Major> getPageContent(int everyPage,int beginPage,int departmentId) throws ClassNotFoundException, SQLException, IOException{
		String sql = "select * from major where departmentId = ? order by name limit ?,?";
		Object []args ={departmentId,(beginPage-1)*everyPage,everyPage};
		return helper.executeQuery(sql, args, this);
	}
	
	
	@Override
	public int getNumsByIdAndProcess(int id,int porcess) throws SQLException{
		String sql="select * from department ,major,member where department.id = member.departmentid and member. majorId=major.id and  member. majorId=? and member.process=?";
		Object []args={id ,porcess};
		return helper.executeCount(sql, args);
	}
	@SuppressWarnings("unchecked")
	@Override
	public int getMajorId(String Major) throws ClassNotFoundException, SQLException, IOException {
		String sql="select id from major where name =?";
		Object [] args={Major};
		List<Integer> list=helper.executeQuery(sql, args, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
                int x=rs.getInt("id");
				return x;
			}
		});
	    return list.isEmpty()?-1:list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Major> findByDepartmentName(String departmentName) throws ClassNotFoundException, SQLException, IOException {
		/*System.out.println(departmentName);*/
		String sql="select * from major,department where major.departmentid= department.id and department.name=? order by major.id";
	    Object[]args={departmentName};
	   /* System.out.println(sql);*/
		List<Major> majors =  helper.executeQuery(sql, args, this);
		/*System.out.println("size:"+majors.size());*/
		/*for(int i=0;i<majors.size();i++){
			System.out.println(majors.get(i).getName());
		}*/
	    return majors;
	}
	
}
